package com.ruixun.shopmanagement.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruixun.shopmanagement.entity.CartItem;
import com.ruixun.shopmanagement.entity.OrderItem;
import com.ruixun.shopmanagement.entity.Orders;
import com.ruixun.shopmanagement.entity.Page2;
import com.ruixun.shopmanagement.entity.Product;
import com.ruixun.shopmanagement.entity.User;
import com.ruixun.shopmanagement.service.inter.OrderService;
import com.ruixun.shopmanagement.util.MyUUID;

@Controller
@RequestMapping("/order")
@ResponseBody
public class OrderController {
	private static final long serialVersionUID = 1L;
	@Autowired
	private OrderService orderService;
	/**
	 * 
	* @Title: submit   
	* @Description:
	* @author: 1246263205@qq.com
	* @date 2019年12月23日 下午2:27:53
	* @param req
	* @param res
	* @return int  0是还未登陆 1是成功 2发生异常
	* @version
	 */
	@RequestMapping("/save")
	public int submit(HttpServletRequest req, HttpServletResponse res) {
		// 验证用户是否登录
		User user = (User) req.getSession().getAttribute("user");
		if (user == null) {
			return 0;
		} // 1.保存订单
			// 获取表单参数
		Orders order = new Orders();
		// 获取表单基本订单信息
		try {
			BeanUtils.populate(order, req.getParameterMap());
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 设置其他订单信息
		order.setOid(MyUUID.getUUID());
		order.setUid(user.getUid());
		order.setOrdertime(new Date());
		order.setState(1);// 已确认
		Double total = 0d;
		// 将购物车信息 拷贝到 订单项中
		Map<String, CartItem> cart = (Map<String, CartItem>) req.getSession().getAttribute("cart");
		List<OrderItem> orderItemList = new ArrayList<>(cart.size());
		Set<Entry<String, CartItem>> entrySet = cart.entrySet();
		for (Entry<String, CartItem> entry : entrySet) {
			// CartItem--> orderItem
			CartItem cartItem = entry.getValue();
			OrderItem orderItem = new OrderItem();
			orderItem.setCount(cartItem.getCount());
			orderItem.setPid(cartItem.getPid());
			orderItem.setOid(order.getOid());
			orderItem.setSubtotal(cartItem.getSubTotal());
			orderItem.setItemid(MyUUID.getUUID());
			orderItemList.add(orderItem);
			total += cartItem.getSubTotal();
		} // 订单总金额
		order.setTotal(total);
		order.setAddress(req.getParameter("address"));  //设置地址
		order.setName(req.getParameter("name"));  //设置联系人姓名
		order.setTelephone(req.getParameter("telephone"));  //设置联系人方式
		if(orderService.save(order, orderItemList)) {
			return 1; //正常
		}
		return 2;	  //系统异常
	}
	

	@RequestMapping("/getMyOrderList")
	public Map<String,Object> getMyOrderList(HttpServletRequest req, HttpServletResponse res) {
		Map<String, Object> respMap = new HashMap<>();
		User user = (User) req.getSession().getAttribute("user");
		//没有登录， 去 登录
		if (user == null) {
			respMap.put("state","0");
			return respMap;
		} 
		Integer currPage = Integer.parseInt(req.getParameter("currPage"));
		boolean asc =Boolean.parseBoolean( req.getParameter("asc"));
		Integer pagesize =Integer.parseInt( req.getParameter("pagesize"));
		//查询出当前用户的订单信息
		Page2 page = orderService.findPage2ByUid(user.getUid(), currPage,asc,pagesize);
		req.setAttribute("page", page);
		if(page!=null) {
			respMap.put("state","1");
			respMap.put("page",page);
		}else {
			respMap.put("state","2");
		}
		return respMap;
	}

}
